package com.dbq.fast.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.business.login.business.WeChatLoginBiz;
import com.dbq.fast.util.ProgressDialogUtil;
import com.dbq.share.ShareUtil;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    public static final String APP_ID = "wxe60d1b76fdcf5504";//wxe60d1b76fdcf5504
    public static final String APP_SECRET = "4ce5ef0c80f8f98225efb55d0c39e268";
    private IWXAPI iwxapi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxentry);

        iwxapi = WXAPIFactory.createWXAPI(this, APP_ID, true);
        iwxapi.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        Log.e("vpn_log", "onReq:"+baseReq.toString());
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.e("vpn_log", "onResp:errCode:"+baseResp.errCode+" type:"+baseResp.getType()+" errStr:"+baseResp.errStr);
        Bundle bundle = new Bundle();
        switch (baseResp.errCode){
            case BaseResp.ErrCode.ERR_OK:
                if(baseResp.getType() == 1) {
                    ProgressDialogUtil.hideProgress();
                    //登录授权成功
                    Message msg = Message.obtain();
                    msg.obj = (SendAuth.Resp)baseResp;
                    WeChatLoginBiz.handler.sendMessage(msg);
                    runOnUiThread(() -> {
                        ProgressDialogUtil.showProgress(WXEntryActivity.this, "", getString(R.string.waiting));
                    });
                }else if(baseResp.getType() == 2) {
                    //分享成功
                    Toast.makeText(App.getContext(), "Share Successfully", Toast.LENGTH_SHORT).show();
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_WECHAT_LOGIN_CANCEL);
                ProgressDialogUtil.hideProgress();
                if(baseResp.getType() == 1){
                    //登录用户未授权
                }else if(baseResp.getType() == 2) {
                    //分享取消
                    Toast.makeText(App.getContext(), "Cancelled", Toast.LENGTH_SHORT).show();
                }
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                YibaAnalysis.getInstance().event(App.getContext(), EventConstant.EVENT_WECHAT_LOGIN_AUTH_DENY);
                //拒绝
                ProgressDialogUtil.hideProgress();
                break;
            default:
                ProgressDialogUtil.hideProgress();
                break;
        }
        ShareUtil.handlerWxShareOnRespData(getApplicationContext(), baseResp);
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        iwxapi.handleIntent(intent, this);
    }

    @Override
    protected void onDestroy() {
        ProgressDialogUtil.hideProgress();
        super.onDestroy();
    }
}
